//
//  RTJUseRrealNameController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/12/3.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJUseRrealNameController.h"

@interface RTJUseRrealNameController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *nameTxt;
@property (weak, nonatomic) IBOutlet UITextField *IDTxt;
@property (weak, nonatomic) IBOutlet UIView *bg_view;
@property (weak, nonatomic) IBOutlet UIView *bg_view1;
@property (weak, nonatomic) IBOutlet UIButton *btn;
@property (weak, nonatomic) IBOutlet UILabel *lab;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewTop;

@end

@implementation RTJUseRrealNameController

- (void)viewDidLoad {
    [super viewDidLoad];
    
     self.viewTop.constant = screenH == 812 ? 88 : 64;
    self.navigationController.navigationBarHidden = NO;
   
    [self backview:_bg_view viewOfcornerRadius:5];
    [self backview:_bg_view1 viewOfcornerRadius:5];
    
    if (_isChangeUserName) {
        
         self.title = @"修改昵称";
        [_btn setTitle:@"修改昵称" forState:UIControlStateNormal];
        
        _lab.text = @"昵称";
        _nameTxt.userInteractionEnabled = YES;
        _nameTxt.placeholder = @"请输入你的昵称";
        _bg_view1.hidden = YES;
        _IDTxt.userInteractionEnabled =NO;
        
    }else{
        
         self.title = @"实名认证";
        _lab.text = @"姓名";
        if ([RTJUserTool getTheUserModel].idcardpassed == 1) {
            
            _btn.hidden = YES;
            _nameTxt.text = [Apputil replaceStringWithAsterisk:[RTJUserTool getTheUserModel].real_name startLocation:1 lenght:[RTJUserTool getTheUserModel].real_name.length-1];
            
            
            NSString *str = [NSString stringWithFormat:@"%ld",(long)[RTJUserTool getTheUserModel].idno];
            
            _IDTxt.text = [Apputil replaceStringWithAsterisk:str startLocation:4 lenght:str.length-8];
            
            _nameTxt.userInteractionEnabled = NO;
            _IDTxt.userInteractionEnabled =NO;
            
        }
    }
    
   
    
    
}

-(void)backview:(UIView *)backview viewOfcornerRadius:(NSInteger)cornerRadius{
    
    backview.layer.cornerRadius = cornerRadius;
    backview.layer.shadowColor = [UIColor colorForHex:@"#dbc9cc"].CGColor;
    backview.layer.shadowOffset = CGSizeMake(-1, 1);
    backview.layer.shadowOpacity = 0.5;
    backview.layer.shadowRadius = 5;
    
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    
    
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    
    
}
- (IBAction)realName:(UIButton *)sender {

    if (_isChangeUserName) {
        
        if (_nameTxt.text.length == 0) {
            
            [SVProgressHUD showErrorWithStatus:@"请填写你的昵称"];
            return;
        }
        [SVProgressHUD showWithStatus:nil];

        [RTJNetWork asyncRequestWithURL:@"user/edit_name" params:@{@"user_name":_nameTxt.text} method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
            
            [SVProgressHUD dismiss];
            
            if (success) {
                
                if ([data[@"status"]integerValue] == 1) {
                    
                    [SVProgressHUD showSuccessWithStatus:@"修改成功"];
                    RTJUserModel *model = [RTJUserTool getTheUserModel];
                    model.user_name = self.nameTxt.text;
                    [RTJUserTool saveUserInfo:model];
                    
                    [self.navigationController popViewControllerAnimated:YES];
                    
                }else{
                    
                    [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"修改失败:%@",data[@"msg"]]];
                    
                }
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"修改失败:%@",errorMsg]];
                
            }
            
        }];
        
        
    }else{
        
        if (_nameTxt.text.length == 0) {
            
            [SVProgressHUD showErrorWithStatus:@"请填写姓名"];
            return;
        }
        
        if (_IDTxt.text.length != 18) {
            
            [SVProgressHUD showErrorWithStatus:@"请正确填写身份证号"];
            return;
        }
        
        [SVProgressHUD showWithStatus:nil];
        
        NSDictionary *dic = @{@"real_name":_nameTxt.text,
                              @"idno":_IDTxt.text
                              };
        
        [RTJNetWork asyncRequestWithURL:@"user/real_name_authentication" params:dic method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
            
            [SVProgressHUD dismiss];
            
            if (success) {
                
                if ([data[@"status"]integerValue] == 1) {
                    
                    [SVProgressHUD showSuccessWithStatus:@"认证成功"];
                    RTJUserModel *model = [RTJUserTool getTheUserModel];
                    model.real_name = self.nameTxt.text;
                    model.idno = [self.IDTxt.text integerValue];
                    model.idcardpassed = 1;
                    [RTJUserTool saveUserInfo:model];
                    [self.navigationController popViewControllerAnimated:YES];
                    
                }else{
                    
                    [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"认证失败:%@",data[@"msg"]]];
                    
                }
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"认证失败:%@",errorMsg]];
                
            }
            
        }];

    }
    
    
    
}



@end
